/*
 * Copyright 2012 Marsrutai.lt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.fragment;

import java.util.ArrayList;

import lt.marsrutai.mobile.android.R;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.vending.billing.IInAppBillingService;
import com.burstly.lib.conveniencelayer.BurstlyBanner;
import com.burstly.lib.conveniencelayer.BurstlyFragment;
import com.google.analytics.tracking.android.EasyTracker;

public class BannerFragment extends BurstlyFragment  {

    private static String BURSTLY_ZONE_ID = "0557122279110284122";
    private static int BURSTLY_REFRESH_RATE = 30;

    private IInAppBillingService mService = null;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.banner_layout, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Boolean bindResult = getActivity().bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);
    }

    private boolean allowBanner() {
        try {
            Bundle ownedItems = mService.getPurchases(3, getActivity().getPackageName(), "inapp", null);
            int response = ownedItems.getInt("RESPONSE_CODE");
            if (response == 0) {
               ArrayList ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
               if (ownedSkus.size() == 0) {
                   return true; // Check succeeded, no donations, allow
               }
            }
            return false;
        }
        catch (Exception e) {
            return false;
        }
    }


    private void initialize() {
        if (allowBanner()) {
            try {
                String bannerViewName = "banner_" + Integer.toString((int)(Math.random() * Integer.MAX_VALUE)); // This is stupid, but need a unique id
                final BurstlyBanner banner = new BurstlyBanner( this, (ViewGroup)getView().findViewById(R.id.banner_viewgroup), null, BURSTLY_ZONE_ID, bannerViewName, BURSTLY_REFRESH_RATE);
                banner.showAd();

                EasyTracker.getTracker().trackEvent("ads", "showAd", "", 0L);
            }
            catch (Exception e) {
                try { EasyTracker.getTracker().trackEvent("ads", "showAd error", "", 0L); }
                catch (Exception ex) { }
            }
        }
    }

    @Override
    public void onDestroyView() {
        if (mServiceConn != null) {
            getActivity().unbindService(mServiceConn);
        }
        super.onDestroyView();
    }


    // Billing service

    ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IInAppBillingService.Stub.asInterface(service);
            BannerFragment.this.initialize();
        }
     };


//     <fragment
//     android:id="@+id/banner_fragment"
//     android:layout_width="fill_parent"
//     android:layout_height="wrap_content"
//     android:name="lt.marsrutai.mobile.android.fragment.BannerFragment" />


}
